package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"project_srvs/user_srv/proto/user"
)

var userClient user.UserClient
var conn *grpc.ClientConn

func Init() {
	var err error
	conn, err = grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		panic(err)
	}
	userClient = user.NewUserClient(conn)
}

func TestGetUserList() {
	rsp, err := userClient.GetUserList(context.Background(), &user.PageInfo{
		Pn:    2,
		PSize: 5,
	})
	if err != nil {
		panic(err)
	}
	for _, userRow := range rsp.Data {
		fmt.Println(userRow.Phone, userRow.NickName, userRow.Pass)
		checkRsp, err := userClient.CheckPassWord(context.Background(), &user.PasswordCheckInfo{
			Password:          "admin123",
			EncryptedPassword: userRow.Pass,
		})
		if err != nil {
			panic(err)
		}
		fmt.Println(checkRsp.Success)
	}
}

func TestUpdateUser() {
	_, err := userClient.UpdateUser(context.Background(), &user.UpdateUserInfo{Id: 2, NickName: "test1", BirthDay: 1072972800})
	//rsp, err := userClient.GetUserList(context.Background(), &user.PageInfo{
	//	Pn:    1,
	//	PSize: 20,
	//})
	////zap.S().Infof("更新用户: %d", 2)
	if err != nil {
		panic(err)
	}
	//for _, userRow := range rsp.Data {
	//	fmt.Println(userRow.Mobile, userRow.NickName, userRow.PassWord)
	//}
}

func TestCreateUser() {
	_, err := userClient.CreateUser(context.Background(), &user.CreateUserInfo{
		NickName: "wang",
		Phone:    "18810758411",
	})
	if err != nil {
		panic(err)
	}
}

func main() {
	Init()
	//TestGetUserList()
	//TestUpdateUser()
	TestCreateUser()
	conn.Close()
}
